summaryrefslogtreecommitdiff
path: root/examples/blog-multiple-authors/src/pages/authors/[author].astro
blob: bb1bc3aa2a16b189cbc1f7fa8f54ddc6a63c83a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
import MainHead from '../../components/MainHead.astro';
import Nav from '../../components/Nav.astro';
import PostPreview from '../../components/PostPreview.astro';
import authorData from '../../data/authors.json';

export async function getStaticPaths() {
	const allPosts = await Astro.glob('../post/*.md');
	let allAuthorsUnique = [...new Set(allPosts.map((p) => p.frontmatter.author))];
	return allAuthorsUnique.map((author) => ({ params: { author }, props: { allPosts } }));
}

const { allPosts } = Astro.props;
const title = 'Don’s Blog';
const description = 'An example blog on Astro';

/** filter posts by author, sort by date */
const posts = allPosts.filter((post) => post.frontmatter.author === Astro.params.author).sort((a, b) => new Date(b.frontmatter.date).valueOf() - new Date(a.frontmatter.date).valueOf());
const author = authorData[posts[0].frontmatter.author];
---

<html lang="en">
	<head>
		<title>{title}</title>
		<MainHead {title} {description} image={posts[0].frontmatter.image} canonicalURL={Astro.canonicalURL.toString()} />

		<style lang="scss">
			.title {
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 3em;
				letter-spacing: -0.04em;
				margin-top: 2rem;
				margin-bottom: 0;
			}

			.avatar {
				width: 1em;
				height: 1em;
				margin-right: 0.5em;
				border-radius: 50%;
				overflow: hidden;

				&-img {
					display: block;
					width: 100%;
					height: 100%;
					object-fit: cover;
				}
			}

			.count {
				font-size: 1em;
				display: block;
				text-align: center;
			}
		</style>
	</head>

	<body>
		<Nav {title} />

		<main class="wrapper">
			<h2 class="title">
				<div class="avatar"><img class="avatar-img" src={author.image} alt="" /></div>
				{author.name}
			</h2>
			{posts.map((post) => <PostPreview post={post} author={author} />)}
		</main>
	</body>
</html>